//
//  MainTabBar.swift
//  SinaWeibo--swift
//
//  Created by 蒋春萌 on 15/9/29.
//  Copyright © 2015年 蒋春萌. All rights reserved.
//

import UIKit

class MainTabBar: UITabBar {

    override func layoutSubviews() {
        
        super.layoutSubviews()
        
//        设置每一个底部item的frame
//        定义一个下标
        var index: CGFloat = 0
        let w = UIScreen.mainScreen().bounds.width / 5
        let h = self.bounds.height
        let frame = CGRectMake(0, 0, w, h)
        
//        遍历子view
        for view in subviews{
        
            if view is UIControl && !(view is UIButton){
            
                view.frame = CGRectOffset(frame, index * w, 0)
                
//                给中间的加号留出位置
                if index == 1{

                    index++
                }
                index++
            }
        }
//        设置加号按钮的frame
        composeButton.frame = CGRectOffset(frame, 2 * w, 0)
    }

//    懒加载一个按钮
    lazy var composeButton:UIButton = {
    
        let btn = UIButton(type: UIButtonType.Custom)
//        设置背景图片
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
//        设置按钮的图片
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Highlighted)
        
        self.addSubview(btn)
        
        return btn
    }()
}
